Page({
  data: {
    TabCur: 0,
    Tab: ["待发货", "已发货"],
  },
  onLoad(options) {
    var that = this;
    that.setData({
      TabCur: options.TabCur || 0,
      identity_id: wx.getStorageSync('identity_id')
    });
  },
  onShow() {
    var that = this;
    if (wx.getStorageSync('identity_id') == 2) {
      wx.redirectTo({
        url: '../business/deliverGoods/deliverGoods',
      })
    } else {
      if (that.data.TabCur == 0) {
        // 避免重新进入当前页面时立即请求造成数据错乱
        that.getOrderList('wait');
      } else if (that.data.TabCur == 1) {
        that.getOrderList('already');
      }
    }
  },
  // 切换选项卡
  tabSelect: function (e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    });
    if (e.currentTarget.dataset.id == 0) {
      this.getOrderList('wait');
    } else if (e.currentTarget.dataset.id == 1) {
      this.getOrderList('already');
    }
  },
  // 获取订单列表
  getOrderList(mold) {
    wx.showLoading({
      title: '正在处理',
    });
    wx.cloud.callFunction({
      name: 'OrderFunctions',
      config: {
        env: this.data.envId
      },
      data: {
        type: 'myOrder',
        mold: mold
      }
    }).then((resp) => {
      wx.hideLoading();
      // console.log(resp);
      this.setData({
        orderList: resp.result.list,
        order_length: 1
      })
    }).catch((e) => {
      console.log(e);
      wx.hideLoading();
    });
  },
  // 跳转到快递信息页面
  toExpress(e) {
    let express_id = e.currentTarget.dataset.express;
    let mobile = e.currentTarget.dataset.mobile;
    let goods_url = e.currentTarget.dataset.goods_url;
    if (express_id != null) {
      wx.navigateTo({
        url: '../customer/express/express?express_id=' + express_id + '&mobile=' + mobile + '&goods_url=' + goods_url,
      })
    } else {
      wx.showToast({
        title: '待发货',
        icon: 'error'
      })
    }
  },
})